import * as API from '../../../api/orderApi';
import * as util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    hiddenPrice: false,
    companyName: '',
    latestNodeList: [],
    allRead: true,
    followRead: true,
    shareId: '',
    // formShare: false,
    order: {
      deliverStatus: ''
    },
    custom: {},
    showShareModal: 0,
    isHidePrice: false,
    attentionPeopleNum: 0,
    browseOrderNum: 0
  },
  getData: function (id) {
    API.getOrderDetail({
      orderId: id
    }).then(res => {
      if (!res) return;
      let data = {
        order: res.order,
        custom: res.custom,
        nodeEventMap: res.nodeEventMap,
        nodes: {},
        companyName: res.company && res.company.name,
        companyId: res.company && res.company.id,
        latestNodeList: res.latestNodeList,
        allRead: res.allRead,
        followRead: res.isRead,
        attentionPeopleNum: res.attentionPeopleNum,
        browseOrderNum: res.browseOrderNum
      };
      wx.stopPullDownRefresh();
      this.setData(data);

    }, err => {
      util.toast('读取订单数据失败，请稍后再试');
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1500);
    })
  },
  switchPriceChange: function (e) {
    this.setData({
      isHidePrice: e.detail.value
    });
    this.previewShare();
  },

  wannaShare: function () {
    this.setData({
      showShareModal: 1,
      isHidePrice: false
    });
    this.previewShare();

  },
  wxShare() {
    this.setData({
      showShareModal: 0
    });
  },
  msgSend(e) {
    util.toast('体验环境不可用');
  },
  copyShortLink(e) {
    wx.showLoading({
      title: '',
    })
    let that = this;
    API.getShortLink({
      customId: this.data.custom.id,
      customName: this.data.custom.name,
      orderList: [{
        orderId: this.data.order.id,
        hiddenPrice: this.data.isHidePrice
      }],
      from: 'detail'
    }).then(res => {
      wx.setClipboardData({
        data: res.shortLink,
        success: function () {
          wx.hideLoading();
          that.setData({
            showShareModal: 0
          })
          if (res.shortLink) {
            wx.showToast({
              title: '短链接已复制',
              icon: 'none',
              duration: 1500
            });
          } else {
            wx.showToast({
              title: '链接获取失败',
              icon: 'none',
              duration: 1500
            });
          }
          that.setData({
            showShareMarker: false
          })
        }
      })
    }, err => {

    });

  },

  gotoProcess: function () {
    wx.navigateTo({
      url: '/pages/order/orderProcess/orderProcess?orderId=' + this.data.orderId + '&shareId=' + this.data.shareId
    })
  },
  cancelShareFirstStep: function () {
    this.setData({
      showShareModal: 0
    })
  },
  prevStep: function () {
    this.setData({
      showShareModal: 1
    })
  },
  nextStep: function () {
    this.setData({
      showShareModal: 2
    })
  },
  previewShare: function () {
    let postBody = [{
      orderId: this.data.order.id,
      customId: this.data.custom.id,
      isHiddenPrice: this.data.isHidePrice,
      platform: "1"
    }];
    API.getWxShare(postBody).then(res => {
      this.setData({
        shareId: res.shareId
      });
    }, err => {
      wx.showToast({
        title: '获取分享id失败',
        icon: 'none'
      })
    })
  },
  phoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.replyPhone
    })
  },
  gotoFollowPepole: function () {
    wx.navigateTo({
      url: '/pages/order/followers/followers?orderId=' + this.data.orderId + '&shardingId=' + this.data.order.shardingId
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showNavigationBarLoading();
    this.setData({
      orderId: options.id || ''
    })
    // this.getData(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    setTimeout(() => {
      wx.hideNavigationBarLoading();
    }, 1200)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu();
    this.getData(this.data.orderId);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData(this.data.orderId);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
    } else {}
    while (true) {
      if (this.data.shareId)
        break;
    }
    return {
      // title: '您好，供应商：' + this.data.companyName + '邀请您查看您的交易订单执行状态',
      title: this.data.companyName + '分享订单全链路详情',
      path: 'pages/share/shareDetail/shareDetail?shareId=' + this.data.shareId,
      imageUrl: '/images_keep/share5.png',
      success: function () {
        this.setData({
          showShareModal: 0
        });
        wx.showToast({
          title: '分享成功'
        })
      }
    }

  }
})